﻿using System;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DealKhuyenMaiV2.com.Models;
using System.IO;
using System.Collections.Generic;

namespace DealKhuyenMaiV2.com.Controllers
{
    [Authorize(Roles = "company")]
    public class CompanyController : BaseController
    {
        //
        // GET: /Company/


        public ActionResult Index()
        {

            return RedirectToAction("AllDeal");
        }
        [HttpGet]
        public ActionResult PostNewDeal()
        {
           //  ViewBag.IDAccount = new SelectList(DbConText.Accounts, "IDAccount", "Email");
            ViewBag.IDCatalogy = new SelectList(DbConText.Catalogies, "IDCatalogy", "NameCatalogy");
            ViewBag.IDProvince = new SelectList(DbConText.Provinces, "IDProvince", "NameProvince");
            return View();
        }
        [HttpPost]
        public ActionResult PostNewDeal(Product p)
        {
          

            if (ModelState.IsValid)
            {

                try
                {

                    //Up picture to Content/Images

                    //lay email doanh nghiep hien tai
                    string email = User.Identity.Name;

                 

                    Account account = DbConText.Accounts.SingleOrDefault(m => m.Email == User.Identity.Name);
                    Product product = new Product()
                    {
                        NameProduct = p.NameProduct,
                        IDProvince = p.IDProvince,
                        Price = p.Price,
                        Discount = p.Discount,
                        Date_start = p.Date_start,
                        Date_end = p.Date_end,
                        IDAccount = account.IDAccount,
                        Description = p.Description,
                        Condition = p.Condition,
                        Summary = p.Summary,
                        Shipping = p.Shipping,
                        DateAdded = DateTime.Now,
                        IDCatalogy = p.IDCatalogy,
                        SortOrder = null,
                        TotalOfNumber = p.TotalOfNumber,
                        NumberOfPurchased = 0,
                        IDStatus = 1,

                    };
                    DbConText.Products.AddObject(product);
                    
                    DbConText.SaveChanges();
                    string numberpicture = Request.Form["NumberofPicture"];
                    List<string> listPathImage = new List<string>();
                    int numpicture = int.Parse(numberpicture);
                    for (int i = 0; i < numpicture; i++)
                    {
                        string NameInput = "product_image[" + (i + 1) + "]";
                        string imagesPath = Request.Form[NameInput];
                        if (imagesPath != null)
                            listPathImage.Add(imagesPath);
                    }

                    foreach (string imgPath in listPathImage)
                    {
                        Image imageNew = new Image();
                        imageNew.IDProduct = product.IDProduct;
                        imageNew.Path = imgPath;
                        DbConText.Images.AddObject(imageNew);

                    }
                    DbConText.SaveChanges();
                    return RedirectToAction("Index");
                }
                catch (Exception ae)
                {

                    ModelState.AddModelError("", ae.Message);
                }
            }
            ViewBag.IDProvince = new SelectList(DbConText.Provinces, "IDProvince", "NameProvince");
            ViewBag.IDCatalogy = new SelectList(DbConText.Catalogies, "IDCatalogy", "NameCatalogy");
            return View();
        }

        public ActionResult ManagePostedDeal()
        {
            return View();
        }

        public ActionResult ManageOrder()
        {
            return View();
        }
        public ActionResult AllDeal()
        {
            Account account = DbConText.Accounts.SingleOrDefault(m => m.Email == User.Identity.Name);

            var alldeal = from p in DbConText.Products where p.IDAccount == account.IDAccount select p;

            return View(alldeal.ToList());
        }

        public ActionResult AllOrder(int productID)
        {

            var allorder = from p in DbConText.Orders where p.IDProduct == productID && p.IDStatus == 4 select p;

            Product product = DbConText.Products.SingleOrDefault(m => m.IDProduct == productID);
            ViewBag.dealname = product.NameProduct + " từ " + product.Date_start + " đến " + product.Date_end;
            decimal sum = 0;
            foreach (Order order in allorder)
            {
                sum += order.Total;

            }

            ViewBag.total = (double)sum;
            //return View(c.ToList());
            return View(allorder.ToList());
            // return View();
        }

        [HttpPost]
        public ActionResult UpdateVoucher(int id)
        {
            Order order = DbConText.Orders.SingleOrDefault(m => m.IDOrder == id);
            order.Voucher = true;
            DbConText.SaveChanges();

            return Json(id);
        }


    }
}
